<?php
/* 
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
/**
 * $Rev$
 * $Id: class.helper.php 46 2012-03-19 16:35:14Z superbounou $
 * $Author: superbounou $
 * $Date: 2012-03-19 16:35:14 +0000 (Mon, 19 Mar 2012) $
 * 
 * Provide component for the application layer
 */
class Helper {

	protected $controller;

	public function __construct($controller) {
		assert('$controller instanceof Action');
		$this->controller = $controller;
	}

	public function tpl($tpl) {
		$tplpath = 'tpl' . DIRECTORY_SEPARATOR . $tpl;
		assert('/*template "' . $tplpath . ' " does not exists*/file_exists($tplpath)');
		$var = '';
		for ($i = 1; $i < func_num_args(); $i++) {
			$data = '_' . $i;
			$$data = func_get_arg($i);
		}
		include ($tplpath);
	}

	public static function link($link) {
		return 'index.php?action=' . $link;
	}

	public static function img($name, $alt = '', $class = '', $id = '') {
		return sprintf('<img src="static/img/%s" alt="%s" %s %s>', $name, $alt, (strlen($id) ? 'id="' . $id . '"' : ''), (strlen($class) ? 'class="' . $class . '"' : ''));
	}

	public static function pic($name, $alt = '', $class = '', $id = '') {
		$ext = end(explode(".", $name));
		assert('/*testing file extension */$ext == \'jpg\' || $ext == \'jpeg\' || $ext == \'png\' || $ext == \'gif\'');
		return self :: img($name, $alt, $class, $id);
	}

	public static function displayDebug($array) {
		if (count($array)>1) {			
			foreach ($array as $line) {
				echo "<li>$line</li>";
			}
		}
	}
}
